package vn.itplus.nguyettm;

import java.util.ArrayList;

import android.content.Context;
import android.provider.MediaStore.Images;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class itemAdapter extends BaseAdapter {

	Context context;// truyen context
	ArrayList<itemModel> list;// luu
	LayoutInflater inflater;// de ve~

	public itemAdapter(Context context, ArrayList<itemModel> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	// nhung doi tuong nao can thay doi thi liet ke het ra o day
	public static class ViewHolder {
		public static TextView txtName;
		public static TextView txtPhone;
		public static ImageView imgPic;
		public static ImageView imgNext;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder viewHolder;
		final itemModel itemmodel = list.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			v = inflater.inflate(R.layout.item, parent, false);
			viewHolder.txtName = (TextView) v.findViewById(R.id.textView1);
			viewHolder.txtPhone = (TextView) v.findViewById(R.id.textView2);
			viewHolder.imgPic = (ImageView) v.findViewById(R.id.imageView1);
			viewHolder.imgNext = (ImageView) v.findViewById(R.id.imageView2);
			v.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) v.getTag();
		}

		if (list.size() > 0) {
			viewHolder.txtName.setText(itemmodel.getName());
			viewHolder.txtPhone.setText(itemmodel.getPhone());
			viewHolder.imgPic.setImageDrawable(itemmodel.getImg());
		} else {
			viewHolder.txtName.setText("No Data");
		}

		return v;
	}
}
